; @@@ START COPYRIGHT @@@
; Licensed to the Apache Software Foundation (ASF) under one
; or more contributor license agreements.  See the NOTICE file
; distributed with this work for additional information
; regarding copyright ownership.  The ASF licenses this file
; to you under the Apache License, Version 2.0 (the
; "License"); you may not use this file except in compliance
; with the License.  You may obtain a copy of the License at
;   http://www.apache.org/licenses/LICENSE-2.0
; Unless required by applicable law or agreed to in writing,
; software distributed under the License is distributed on an
; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
; KIND, either express or implied.  See the License for the
; specific language governing permissions and limitations
; under the License.
; @@@ END COPYRIGHT @@@
;
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "Trafodion ODB 2.3"
#define MyAppVersion "2.3.0"
#define MyAppPublisher "Apache Trafodion"
#define MyAppURL "http://trafodion.apache.org/"
#define MyAppExeName "odb.exe"
#define BUILDDIR GetEnv('BUILDDIR')

[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{01D799A9-AA76-4CE2-8D9D-D0C88B3EA241}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={pf}\Trafodion\{#MyAppName}
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
OutputBaseFilename=TRAFODB-{#MyAppVersion}
Compression=lzma
SolidCompression=yes
ArchitecturesInstallIn64BitMode=x64
UninstallDisplayName={#MyAppName}
ArchitecturesAllowed=x64
SetupLogging=yes

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Files]
Source: "{#BUILDDIR}\odb\x64\Release\odb.exe"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"

[UninstallDelete]
Type: filesandordirs; Name: "{app}"

[Code]
function GetUninstallString(): string;
var
  sUnInstPath: string;
  sUnInstPath_is1: string;
  sUnInstallString: String;
  sAppId: String;
  sAppId_is1: String;
begin
  Result := '';
  sAppId := ExpandConstant('{#emit SetupSetting("AppId")}');
  sAppId_is1 := ExpandConstant('{#emit SetupSetting("AppId")}_is1');
  sUnInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\') + sAppId;
  sUnInstPath_is1 := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\') + sAppId_is1;

  sUnInstallString := '';
  if not RegQueryStringValue(HKLM, sUnInstPath, 'UninstallString', sUnInstallString) then
    if not RegQueryStringValue(HKCU, sUnInstPath, 'UninstallString', sUnInstallString) then
      if not RegQueryStringValue(HKLM, sUnInstPath_is1, 'UninstallString', sUnInstallString) then
        RegQueryStringValue(HKCU, sUnInstPath_is1, 'UninstallString', sUnInstallString);
  Result := sUnInstallString;
end;

function IsUpgrade: Boolean;
begin
  Result := (GetUninstallString() <> '');
end;

function InitializeSetup: Boolean;
var
  V: Integer;
  iResultCode: Integer;
  sUnInstallString: string;
  sAppId: String;
  sAppId_is1: String;
  sAppName: String;
begin
  Result := True; // in case when no previous version is found
  sAppId := ExpandConstant('{#emit SetupSetting("AppId")}');
  sAppId_is1 := ExpandConstant('{#emit SetupSetting("AppId")}_is1');
  sAppName := ExpandConstant('{#emit SetupSetting("AppName")}');
  sUnInstallString := GetUninstallString();
  if (sUnInstallString <> '') then  //Your App GUID/ID
  begin
    V := MsgBox(ExpandConstant('An existing install of '+ sAppName + ' was detected.' + #13#10#13#10 +'The installer will now uninstall the old version and install the new version.' + #13#10#13#10 +'Do you want to continue?'), mbInformation, MB_YESNO); //Custom Message if App installed
    if V = IDYES then
    begin
      sUnInstallString :=  RemoveQuotes(sUnInstallString);
      if (Pos('msiexec',Lowercase(sUnInstallString)) <> 0) then
      begin
        if not Exec('msiexec.exe', '/X'+sAppId+' /qn', '', SW_SHOW, ewWaitUntilTerminated, iResultCode) then
        begin
        MsgBox('Failed to uninstall existing files.' + #13#10 + ' ' + SysErrorMessage(iResultCode), mbError, MB_OK);
        Result := False;
        end
      end
      else
      begin
        if not Exec(sUnInstallString, '/SILENT', '', SW_SHOW, ewWaitUntilTerminated, iResultCode) then
        begin
        MsgBox('Failed to uninstall existing files.' + #13#10 + ' ' + SysErrorMessage(iResultCode), mbError, MB_OK);
        Result := False;
        end
        else

        Result := True; //if you want to proceed after uninstall
                  //Exit; //if you want to quit after uninstall
     end
     end
    else
      Result := False; //when older version present and not uninstalled
  end;

end;
